//
//  UITextField+HWCategory.m
//  OCDome
//
//  Created by 李含文 on 2020/8/17.
//  Copyright © 2020 李含文. All rights reserved.
//

#import "UITextField+HWCategory.h"
#import <objc/runtime.h>

@interface UITextField()

@property (nonatomic, copy) void(^hw_editingChangedBlock)(NSString *content);

@property (nonatomic, copy) void(^hw_editingDidEndBlock)(NSString *content);
@end

@implementation UITextField (HWCategory)
//MARK: - get/set关联
- (void (^)(NSString *))hw_editingChangedBlock {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setHw_editingChangedBlock:(void (^)(NSString *))hw_editingChangedBlock {
    objc_setAssociatedObject(self, @selector(hw_editingChangedBlock), hw_editingChangedBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(NSString *))hw_editingDidEndBlock {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setHw_editingDidEndBlock:(void (^)(NSString *))hw_editingDidEndBlock {
    objc_setAssociatedObject(self, @selector(hw_editingDidEndBlock), hw_editingDidEndBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
//MARK: -
- (void)hw_editingChanged:(void(^)(NSString *content))action {
    self.hw_editingChangedBlock = action;
    [self addTarget:self action:@selector(textDidChange) forControlEvents:(UIControlEventEditingChanged)];
}
- (void)hw_editingDidEnd:(void(^)(NSString *content))action {
    self.hw_editingDidEndBlock = action;
    [self addTarget:self action:@selector(editingDidEnd) forControlEvents:(UIControlEventEditingDidEnd)];
}
- (void)textDidChange {
    if (self.hw_editingChangedBlock) {
        self.hw_editingChangedBlock(self.text);
    }
}
- (void)editingDidEnd {
    if (self.hw_editingDidEndBlock) {
        self.hw_editingDidEndBlock(self.text);
    }
}

@end
